package Epic;

/**
 * Given a string.Replace the words whose lengt>=4 and is even,
 * with a space between the two equal halves of the word.
 * consider only alphabets for finding the eveness of the word 

	I/P "A person can't walk in this street" 
	O/P "A per son ca n't wa lk in th is str eet"
 * @author Ray
 *
 */
public class StringSplit {
	
	public static String split(String s){
		if(s==null || s.length()==0) return "";
		String[] strs = s.split(" ");
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<strs.length;++i){
			String cur = strs[i];
			int count = 0;
			for(int j=0;j<cur.length();++j){
				if(cur.charAt(j)>='A' && cur.charAt(j)<='z'){
					count++;
				}
			}
			if(count>=4 && count%2==0){
				count = count/2;
				for(int j=0;j<cur.length();++j){
					sb.append(cur.charAt(j));
					if(cur.charAt(j)>='A' && cur.charAt(j)<='z'){
						count--;
					}
					if(count==0)
						sb.append(" ");
				}
			}
			else{
				sb.append(strs[i]);
			}
			
			if(i<strs.length-1)
				sb.append(" ");
		}
		
		return sb.toString();
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String test = "A person can't walk in this street";
		System.out.println(split(test));
		
	}

}
